home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 32 / Mac Magazin and MacEasy Magazine CD - Issue 32.iso / Multimedia / PlayerPRO 4.5.5 Dev.Kit / Plug-Ins / Sound Filters Plugs / Echo.c < prev    next >
C/C++ Source or Header  |  1995-10-08  |  5KB  |  210 lines

  1. /*    Echo            */
  2. /*    v 0.3            */
  3. /*    1995 by Liane    */
  4.  
  5. //    Usage:
  6. //    Simulates an echo.
  7. //    You can set the timing (ms), which is computed
  8. //    for a F#5.
  9. //    Works on the selected part or all the waveform
  10. //    if there is no selection.
  11.  
  12. #include <Dialogs.h>
  13. #include "MAD.h"
  14. #include "PPPlug.h"
  15.  
  16. #if defined(powerc) || defined(__powerc)
  17. enum {
  18.         PlayerPROPlug = kCStackBased
  19.         | RESULT_SIZE(SIZE_CODE( sizeof(OSErr)))
  20.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( sData*)))
  21.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( long)))
  22.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( long)))
  23.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( PPInfoPlug*)))
  24. };
  25.  
  26. ProcInfoType __procinfo = PlayerPROPlug;
  27. #else
  28. #include <A4Stuff.h>
  29. #endif
  30.  
  31.  
  32. #define tdelay        3
  33. #define tstrength    4
  34. #define enclosure    9
  35.  
  36. GDHandle    TheGDevice:0xCC8;
  37.  
  38. void AutoPosition( DialogPtr aDia)
  39. {
  40.     Point    Position, mouse;
  41.     Rect    ViewRect;
  42.     short    XSize = (aDia->portRect.right - aDia->portRect.left), YSize = (aDia->portRect.bottom - aDia->portRect.top);
  43.  
  44.  
  45.     GetMouse( &mouse);
  46.     LocalToGlobal( &mouse);
  47.  
  48.     SetRect( &ViewRect, (*TheGDevice)->gdRect.left + 8, (*TheGDevice)->gdRect.top + 43,
  49.                         (*TheGDevice)->gdRect.right - 8, (*TheGDevice)->gdRect.bottom - 8);
  50.  
  51.     Position.h = mouse.h - XSize/2;
  52.     if( Position.h + XSize >= ViewRect.right) Position.h = ViewRect.right - XSize;
  53.     else if( Position.h <= ViewRect.left) Position.h = ViewRect.left;
  54.  
  55.     Position.v = mouse.v - YSize/2;
  56.     if( Position.v + YSize >= ViewRect.bottom) Position.v = ViewRect.bottom - YSize;
  57.     else if( Position.v <= ViewRect.top) Position.v = ViewRect.top;
  58.  
  59.     MoveWindow( aDia, Position.h, Position.v, false);
  60.  
  61.     ShowWindow( aDia);
  62. }
  63.  
  64. void raiseRect (Rect theRect)
  65. {
  66.     ForeColor(whiteColor);
  67.     MoveTo(theRect.left,theRect.bottom);
  68.     LineTo(theRect.left,theRect.top);
  69.     LineTo(theRect.right,theRect.top);
  70.     ForeColor(blackColor);
  71.     LineTo(theRect.right,theRect.bottom);
  72.     LineTo(theRect.left,theRect.bottom);
  73. }
  74.  
  75. #define timeConvert        22254 //≈22KHZ
  76.  
  77. pascal void xRectProc (WindowPtr    theWindow,
  78.                         short        theItem)
  79. {
  80. short    iType;
  81. Handle    iHandle;
  82. Rect    iRect;
  83.  
  84.     GetDItem(theWindow,theItem,&iType,&iHandle,&iRect);
  85.     raiseRect( iRect);
  86. }
  87.  
  88. Boolean getParams ( short dlgID, long *p1, long *p2, PPInfoPlug *thePPInfoPlug)
  89. {
  90. DialogPtr    theDialog;
  91. Boolean        theResult = false;
  92.  
  93.     theDialog = GetNewDialog(dlgID,nil,(WindowPtr)-1);
  94.     if (theDialog) {
  95.         short    iType, itemHit;
  96.         Handle    iHandle;
  97.         Rect    iRect;
  98.         Str255    textStr;
  99.         
  100.         SetPort( theDialog);
  101.         AutoPosition( theDialog);
  102.         GetDItem(theDialog,enclosure,&iType,&iHandle,&iRect);
  103.         SetDItem(theDialog,enclosure,iType,(Handle)xRectProc,&iRect);
  104.         GetDItem(theDialog,tdelay,&iType,&iHandle,&iRect);
  105.         NumToString(*p1,textStr);
  106.         SetIText(iHandle,textStr);
  107.         GetDItem(theDialog,tstrength,&iType,&iHandle,&iRect);
  108.         NumToString(*p2,textStr);
  109.         SetIText(iHandle,textStr);
  110.         SelIText(theDialog,tdelay,0,32767);
  111.  
  112.     //    SetDialogDefaultItem(theDialog,1);
  113.  
  114.         do
  115.         {
  116.             ModalDialog( (ModalFilterProcPtr) thePPInfoPlug->MyDlgFilterUPP, &itemHit);
  117.         }
  118.         while ((itemHit != ok) && (itemHit != cancel));
  119.         
  120.         if (itemHit == ok)
  121.         {
  122.             theResult = true;
  123.             GetDItem(theDialog,tdelay,&iType,&iHandle,&iRect);
  124.             GetIText(iHandle,textStr);
  125.             StringToNum(textStr,p1);
  126.             GetDItem(theDialog,tstrength,&iType,&iHandle,&iRect);
  127.             GetIText(iHandle,textStr);
  128.             StringToNum(textStr,p2);
  129.         }
  130.         DisposDialog(theDialog);
  131.     }
  132.     return theResult;
  133. }
  134.  
  135. int checkMax (int v)
  136. {
  137.     if( v >= 127) return 127;
  138.     else if( v <= -127 ) return -127;
  139.     else return v;
  140. }
  141.  
  142. OSErr main(     sData                    *theData,
  143.                 long                    SelectionStart,
  144.                 long                    SelectionEnd,
  145.                 PPInfoPlug                *thePPInfoPlug)
  146. {
  147. long    i, length,
  148.         temp1, temp2,
  149.         pDelay = 250, pStrength = 50;
  150.  
  151.     if (getParams (5000, &pDelay, &pStrength, thePPInfoPlug))
  152.     {
  153.         if (SelectionStart == SelectionEnd)
  154.         {
  155.             SelectionStart = 0;
  156.             SelectionEnd = theData->size;
  157.         }
  158.         length = SelectionEnd - SelectionStart - 1;
  159.  
  160.         pDelay = (pDelay * timeConvert) / 1000;    //convert ms to samples
  161.  
  162.         switch( theData->amp)
  163.         {
  164.             case 8:
  165.             {
  166.                 Ptr    orgPtr = (theData->data) + SelectionStart, destPtr = orgPtr + pDelay;
  167.                 
  168.                 for( i = 0; i < (length - pDelay); i++)
  169.                 {
  170.                     temp1 = *orgPtr++;
  171.                     temp1 = (pStrength * temp1) / 100;
  172.                     
  173.                     temp2 = *destPtr;
  174.                     
  175.                     temp1 += temp2;
  176.  
  177.                     if( temp1 >= 127) temp1 = 127;    // overflow ?
  178.                     else if( temp1 <= -128 ) temp1 = -128;
  179.                     
  180.                     *destPtr++ = temp1;
  181.                 }
  182.             }
  183.             break;
  184.  
  185.             case 16:
  186.             {
  187.                 short    *orgPtr = (short*) theData->data + (SelectionStart / 2),
  188.                 *destPtr = orgPtr + pDelay;
  189.                 
  190.                 for( i = 0; i < length / 2 - pDelay; i++)
  191.                 {
  192.                     temp1 = *orgPtr++;
  193.                     temp1 = (pStrength * temp1) / 100;
  194.                     
  195.                     temp2 = *destPtr;
  196.                     
  197.                     temp1 += temp2;
  198.                     
  199.                     if( temp1 >= (short)0x7FFF) temp1 = 0x7FFF;    // overflow ?
  200.                     else if( temp1 <= (short)0x8000 ) temp1 = (short)0x8000;
  201.                     
  202.                     *destPtr++ = temp1;
  203.                 }
  204.             }
  205.             break;
  206.         }
  207.     }
  208.     
  209.     return noErr;
  210. }